﻿imports System.ComponentModel
Public Class Wallpaper
	Inherits DbBased
	Implements IChangeSender
	
	Protected _filename As String
	Protected _date As Date
	Protected _orgaid As Integer
	Protected _name As String
	Protected _resolution As String
	Protected _true_name As String
	Protected _id As Integer
	Protected _set As Integer
	Protected _favorite As String
	
	Protected _wp_manager As WallpaperService = ServiceManager.GetServiceManager.GetWallpaperService
	
	Protected _listener As New List(Of IChangeListener)
	
	Private Const SPI_SETDESKWALLPAPER As Integer = &H14
	Private Const SPIF_UPDATEINIFILE As Integer = &H1
	Private Const SPIF_SENDWININICHANGE As Integer = &H2
	
	Private Declare Auto Function SystemParametersInfo Lib "user32.dll" ( _
		ByVal uAction As Integer, ByVal uParam As Integer, _
		ByVal lpvParam As String, ByVal fuWinIni As Integer) As Integer
	
	' change this to whatever filename you want to use
	Const WallpaperFile As String = "MovieCollectionImage.bmp"
	
	Public Sub New(ByVal wp As WallpaperService)
	End Sub
	
	<DisplayName("In Favoriten")> _
	Public Property Favorite() As String
		Get
			Return _favorite
		End Get
		Set(ByVal value As String)
			_favorite = value
		End Set
	End Property
	
	<Browsable(false)> _
	Public ReadOnly Property DbFavorite() As String
		Get
			If _favorite = True Then
				Return "Y"
			Else
				Return "N"
			End If
		End Get
	End Property
	
	<DisplayName("Verwendet")> _
	Public Property Gesetzt() As Integer
		Get
			Return _set
		End Get
		Set(ByVal value As Integer)
			_set = value
		End Set
	End Property
	
	<Browsable(false)> _
	Public Property ID() As Integer
		Get
			Return _id
		End Get
		Set(ByVal value As Integer)
			_id = value
		End Set
	End Property
	
	<DisplayName("Name")> _
	Public Property FullName() As String
		Get
			Return _true_name
		End Get
		Set(ByVal value As String)
			_true_name = value
		End Set
	End Property
	
	<DisplayName("Auflösung")> _
	Public Property Resolution() As String
		Get
			Return _resolution
		End Get
		Set(ByVal value As String)
			_resolution = value
		End Set
	End Property
	
	<Browsable(false)> _
	Public Property Name() As String
		Get
			Return _name
		End Get
		Set(ByVal value As String)
			_name = value
			If _true_name = "" then
				_true_name = Utils.InitCap(_name.Replace("-"," "))
			End If
		End Set
	End Property
	
	<Browsable(false)> _
	Public Property OrgaID() As Integer
		Get
			Return _orgaid
		End Get
		Set(ByVal value As Integer)
			_orgaid = value
		End Set
	End Property
	
	<DisplayName("Heruntergeladen am")> _
	Public Property Datum() As Date
		Get
			Return _date
		End Get
		Set(ByVal value As Date)
			_date = value
		End Set
	End Property
	
	<DisplayName("Dateiname")> _
	Public Property Filename() As String
		Get
			Return _filename
		End Get
		Set(ByVal value As String)
			If value.IndexOf("RESOLUTION") > 0 And _resolution <> "" Then
				value = value.Replace("RESOLUTION", _resolution)
			End If
			_filename = value
		End Set
	End Property
	
	Public Sub SetzeWallpaper()
		Dim wallplocation As String
		Dim imagelocation As String = Configuration.GetObject.SkinsPath & _filename
		Dim b As Bitmap
		
		b = New Bitmap(imagelocation)
		
		wallplocation = My.Computer.FileSystem.CombinePath( _
			My.Computer.FileSystem.SpecialDirectories.MyPictures, _filename)
		Try
			b.Save(wallplocation, System.Drawing.Imaging.ImageFormat.Bmp)
			SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, wallplocation, _
				SPIF_UPDATEINIFILE Or SPIF_SENDWININICHANGE)
			_wp_manager.IncrementWallpaperSet(Me)
			notifyChanges()
		Catch Ex As Exception
			MsgBox("There was an error setting the wallpaper: " & Ex.Message)
		End Try
	End Sub
	
	Public Sub AddListener(ByVal icl As IChangeListener) Implements IChangeSender.AddListener
		_listener.Add(icl)
	End Sub
	
	Public Sub RemoveListener(ByVal icl As IChangeListener) Implements IChangeSender.RemoveListener
		_listener.Remove(icl)
	End Sub
	
	Public Sub notifyChanges()
		For Each icl As IChangeListener In _listener
			icl.NotifyChange(True)
			icl.DoChange(Me)
		Next
	End Sub
	
End Class
